// Address book update program
// This simple program creates and updates an address book
//  with up to 500 entries.
// It is written as an example of using  a RANDOM file and 
//  using INPUTDIALOG and UPDATEDIALOG to update it.
// It can find records by searching for any string 
//  or sub-string in the first or last name fields.
// Once found, it can be updated or deleted.
// This program could be modified (by you) to import lists of
//  names, print labels, etc., or just use it as a handy
//  address book on your computer.

// Field statement to define the record:
FIELD #2, 15 as first$, 2 as mi$, 25 as last$, 30 as address$, 6 as apt$, 30 as city$, 5 as state$, 10 as zip$, 15 as phone$, 30 as email$

DIM	dialog_array$  (14, 5)
DIM	index_key$ (501), index_entry (501)
unused$ = "(unused)        "   // (length is important) 

rc = load_up_array ()   	// load up the Dialogarray

// See if the address book file exists by tying to open it
//  as a sequential file.

 rc = open ("input", 2, "address book.data",170)
 close 2
 if rc = 0 then goto 50  // if file already exists

 // otherwise, we create it

 open random, 2, "address book.data", 170
 // Load up the file with 500 "unused" records
 print #2 :  	// clear out all the fields
 last$ = "(unused)"
 for i=1 to 500
   put #2, i // put same data to each record
 next
 close 2
 print "file created"

50 // file exists now
 open random, 2, "address book.data", 170

// Read in all the keys
// The keys (in an array) help to make searches faster so that
//  you don't have to read the entire data file to search it.
for i=1 to 500
  get #2, i
  key$ = left$(last$,10)+left$(first$,6)
  key$ = tolower$ (key$) // make keys lower case
  index_key$(i) = key$
  index_entry(i) = i
next

// Now see if user wants to add a record or look something up

100			// Ask what to do next

 rc = messagebox ("Address Book\n\nWhat do you want to do?", "Add Entry", "Look up", "Browse")

 if rc < 1 then goto 999  // quit
 if rc = 1 then goto 200  // add
 if rc = 2 then goto 300  // look up
 if rc = 3 then goto 400  // browse

200				// add a record

// Find an empty entry
 index = -1
 for i = 1 to 500
   if index_key$ (i) <> unused$ then goto 65
   index = i
   i = 501 // break out
65
 next
 if index <> -1 then goto 150
 Beep 
 rc = Messagebox ("Database is full - can't add anymore entries.", "OK")
 goto 100

150
 rc = inputdialog ("Enter Info for New Entry", dialog_array$)
 if rc <> 5 and rc <> 0  then goto 100  // not update or Enter
 key$ = left$(last$,10)+left$(first$,6)
 key$ = tolower$ (key$)
 index_key$(index) = key$
 put #2, index
 goto 100

300 			// Look Up

 inputdialog "Enter Name to Search For"; search_name$
 search_name$ = tolower$ (search_name$)
 //search for the name
 next_index = 1

310 // start or continue search
 index = -1
 for i = next_index to 500
   if instr (index_key$ (i),search_name$) = 0 then goto 320
   if index_key$ (i) = unused$    then goto 320
   index = i 
   next_index = i+1
   break
320
 next

if (index <> -1) then goto 350
 rc = messagebox ("No Match (or no more matches)", "Try Again", "Cancel")
 if rc = 1 then goto 300 else goto 100

350 // some kind of a match found
 get #2, index
 rc = updatedialog ("Update Address List Entry",dialog_array$)
 if rc <> 5 then goto 360  // if not Update
 // Update.  
 key$ = left$(last$,10)+left$(first$,6)  // fix up key
 key$ = tolower$ (key$)			 // make lower case
 index_key$(index) = key$		 // update keu in array
 put #2, index				 // Update file record
 goto 100

360 if rc <> 4 and rc <> 0 then goto 370 
 // Skip or Enter
 goto 310  // keep searching

370 
 if rc <> 3 then goto 100
          
 // delete

 rc = messagebox ("Are you sure you want to delete this entry?", "Yes", "No")
 if rc <> 1 then goto 350  // if not "yes", re-display
 index_key$(index) = unused$
 print #2
 last$ = "(unused)"
 put #2, index

goto 100 

400				// Browse

search_name$ = "" // search will return all unused entries
next_index = 1 
goto 310

999 				// Exit

close 2
print "Good Bye.  Thanks for using the Addres Book"
end

//-------------------------------------------------------
// DATA statements to use in the INPUTDIALOG array:

DATA   first$,    First Name,  	0, 0,   100
DATA   mi$,	  M.I.,		0, 40,	100
DATA   last$,	  Last Name,	0, 50,	100	 
DATA   address$,  Address, 	1, 0,   100
DATA   apt$,      Apt,     	1, 80,  100
DATA   city$,     City,    	2, 0,   100
DATA   state$,    State,   	2, 65,  100
DATA   zip$,      Zip,     	2, 75,  100
DATA   phone$,    Phone,   	3, 0,   100
DATA   email$,    E-mail,   	4, 0,   100
DATA   3,	  Delete,	5, 0,  	100
DATA   4,	  Next,		5, 27,  100
DATA   5,	  Update,	5, 52,  100
DATA   6,	  Cancel,	5, 77,  100
//-------------------------------------------------------
def load_up_array () = gosub 9000
9000
FOR i = 0 to 13
FOR j = 0 to 4
READ dialog_array$ (i, j)	// load up the Dialogarray
NEXT
NEXT
return 0


